package com.lm.liaoming.base;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lm.liaoming.ActivtyManager;
import com.lm.liaoming.R;
import com.lm.liaoming.tool.Toaster;
import com.lm.liaoming.tool.Utils;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 韦敏敏
 */
public abstract class BaseActivity extends AppCompatActivity implements IToaster, IUserState{

    private Toolbar toolbar;
    private TextView toolbarTitle;
    private ImageView toolbarMenu;
    /**
     * 设置要显示的Layout ID
     *
     * @return
     */
    public abstract int setLayoutId();


    /**
     * 设置顶部 Toolbar 的 title
     *
     * @param id
     */
    public void setToolbarTitle(int id) {
        if (null != toolbarTitle) {
            toolbarTitle.setText(getResources().getText(id));
        }
    }

    /*结束非当前的activity*/
    public void finishOtherActivity(Activity clazz) {
        ActivtyManager.finishOtherActivity(clazz);
    }

    /**
     * 设置顶部 Toolbar 的 title
     *
     * @param title
     */
    public void setToolbarTitle(String title) {
        if (null != toolbarTitle) {
            toolbarTitle.setText(title);
        }
    }

    /**
     * 设置顶部 Toolbar 的 title
     *
     * @param title
     */
    public void setToolbarTitleSpannable(SpannableString title) {
        if (toolbarTitle != null) {
            toolbarTitle.setText(title);
        }
    }

    /**
     * 设置顶部 Toolbar 的 title
     *
     * @param id
     */
    public void setToolbarTitleBackground(int id) {
        if (toolbarTitle != null) {
            toolbarTitle.setBackgroundDrawable(getResources().getDrawable(id));
        }
    }

    /**
     * 设置toolbar背景(导航的Back,左边按钮)
     *
     * @param resId
     */
    public void setNavigationbarBackground(int resId) {
        if (null != toolbar) {
            toolbar.setNavigationIcon(resId);
        }
    }

    /**
     * 获取toolbar(导航的Back,左边按钮)
     *
     * @return
     */
    public Toolbar getToolbar() {
        return toolbar;
    }

    /**
     * 隐藏右边菜单
     */
    public void hideRightMenu() {
        if (toolbarMenu != null) {
            toolbarMenu.setVisibility(View.GONE);
        }
    }

    /**
     * 显示右边菜单
     */
    public void showRightMenu() {
        if (toolbarMenu != null) {
            toolbarMenu.setVisibility(View.VISIBLE);
        }
    }


    /**
     * 设置标题文字颜色
     *
     * @param colorId
     */
    public void setToolbarTitleTextColor(int colorId) {
        toolbarTitle.setTextColor(colorId);
    }

    /**
     * 获取工具菜单对象(导航的右边按钮)
     *
     * @return
     */
    public ImageView getToolbarRightMenu() {
        return toolbarMenu;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ActivtyManager.addActivity(this);
        super.onCreate(savedInstanceState);
        initVariables();
        if (setLayoutId() != 0) {
            setContentView(setLayoutId());
            toolbar = this.findViewById(R.id.toolbar);
            if (null != toolbar) {
                toolbar.setTitle("");
                toolbarTitle = toolbar.findViewById(R.id.toolbar_title);
                toolbarMenu = toolbar.findViewById(R.id.toolbar_menu_right);
                setSupportActionBar(toolbar);
                toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ActivtyManager.finishCurrentActivity();
                    }
                });
            }
        }
        initViews();
    }

    /**
     * finish当前Activity
     */
    public void finishCurrentActivity() {
        ActivtyManager.finishCurrentActivity();
    }


    /**
     * finish所有Activity
     */
    public void finishAllActivity() {
        ActivtyManager.finishAllActivity();
        // 软件彻底退出时，关闭线程池，防止内存泄漏导致OOM
        Utils.executorService.shutdown();
    }

    /**
     * 结束指定的activity
     *
     * @param clazz
     */
    public void finishActivity(Class clazz) {
        ActivtyManager.finishActivity(clazz);
    }


    @Override
    public void showToast(String msg) {
        Toaster toaster = new Toaster(this.getApplicationContext(), msg);
        toaster.showLoading();
        //        if (!isFinishing()) {
        //            DialogManager.showToath(this,msg);
        //        }
    }

    /**
     * toast为资源文件
     *
     * @param resId
     */
    public void showToast(int resId) {
        Toaster toaster = new Toaster(this, this.getString(resId));
        toaster.showLoading();
        //        if (!isFinishing()) {
        //            DialogManager.showToath(this,resId+"");
        //        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        //        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        //        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }


    @Override
    public void finish() {
        super.finish();
        //        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }


    public abstract void initVariables();

    public abstract void initViews();
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10001) {
            Toast.makeText(this, "从开启权限的页面转跳回来", Toast.LENGTH_SHORT).show();

        }
    }
}
